﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CaiyinSoft.NetGather.Protocol;

namespace CaiyinSoft.NetGather.NetGatherLib.Filter.Process
{
    [Serializable]
    public class SimpleReplaceHTMLAnalyze : HTMLFilterBase
    {



        public override string FilterCaption
        {
            get
            {
                return "文本替换过滤器";
            }
        }

        public override string ExpressionCaption
        {
            get
            {
                return "";
            }
        }

        public override string Description
        {
            get
            {
                return "把原始字符串替换成目标字符串";
            }
        }


        //public int ProcessType { get; private set; }
        public string ReplaceFrom { get; set; }
        public string ReplaceTo { get; set; }


        public SimpleReplaceHTMLAnalyze():base()
        {
            _InitParams();
        }

        private void _InitParams()
        {
            SetInputHTML("");
            OutputKey = Guid.NewGuid().ToString();
            //OutputType = 0;
            //ProcessType = 0;
        }

        public override List<FilterResult> FilterHTML()
        {
            int parentHTMLPosition = 0;
            if (ParentFilter != null)
                parentHTMLPosition = ParentFilter.HTMLPosition;
            HTMLPosition = parentHTMLPosition;

            List<FilterResult> parentResults = GetInputHTML();
            if (parentResults == null || parentResults.Count == 0) return null;
            List<FilterResult> listRevalue = new List<FilterResult>();
            foreach (var pr in parentResults)
            {
                if (ReplaceFrom == null || ReplaceTo == null)
                {
                    listRevalue.Add(pr);
                }
                else
                {
                    FilterResult result = new FilterResult(pr.HTMLPosition, pr.HTMLResult.Replace(ReplaceFrom, ReplaceTo));
                    listRevalue.Add(result);
                }
            }
            return listRevalue; // 
        }

        public override IHTMLFilter Clone()
        {

            SimpleReplaceHTMLAnalyze filter = new SimpleReplaceHTMLAnalyze();
            foreach (var f in this.ChildFilters)
            {
                var fc = f.Clone();
                fc.ParentFilter = filter;
                filter.ChildFilters.Add(fc);
            }
            filter.Runabled = this.Runabled;
            filter.RunOnce = this.RunOnce;
            filter.OutputKey = this.OutputKey;
            filter.FilterExpression = this.FilterExpression;

            filter.ReplaceFrom = this.ReplaceFrom;
            filter.ReplaceTo = this.ReplaceTo;
            return filter;
        }

        [NonSerialized]
        private P000_SimpleReplaceHTMLAnalyzeForm _editform;
        public override InnerFormBase GetEditForm()
        {
            if (_editform == null)
            {
                _editform = new P000_SimpleReplaceHTMLAnalyzeForm();
                _editform.InfoSaveEventHandler += (sender, args) =>
                {
                    this.RunOnce = _editform.RunOnce;
                    this.OutputKey = _editform.OutputKey;
                    this.ReplaceFrom = _editform.ReplaceFrom;
                    this.ReplaceTo = _editform.ReplaceTo;

                };
            }

            _editform.HTMLFilter = this;
            return _editform;
        }


    }
}
